﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

using BitcoinCommon;

namespace BitcoinExchange.Wallet
{
    public class BTCeWallet
    {
        MainInfo _info;

        public double KRWAmount;
        public double USDAmount;
        public double BTCAmount;

        public BTCeWallet(MainInfo info)
        {
            _info = info;
            KRWAmount = 0.0;
            USDAmount = 0.0;
            BTCAmount = 0.0;
        }

        public void RetrievedWallet()
        {
            if (!_info.IsLogin())
                return;

            string strData = "";
            string strURL;
            string strParameter;

            Dictionary<string, string> dicURLParameter = _info.GetBTCURLParameterAccessKey();
            dicURLParameter.Add("method", "getInfo");

            strURL = _info.GetBTCeURL() + StringURL.URL_BTCE_WALLET;
            strParameter = _info.ConvertURLParameter(dicURLParameter);

            strData = _info.RequestData(strURL, strParameter, _info.GetHttpHeaderAPI(strParameter));

            if (strData.Length == 0)
                return;

            ConvertToData(strData);
        }

        private void ConvertToData(string strData)
        {

            JObject objData;

            if (strData.Length == 0)
                return;

            objData = JObject.Parse(strData);
            JToken objToken;
            //계좌 잔고 정보를 추출
            objToken = objData["return"];
            objToken = objToken["funds"];

            try
            {
                USDAmount = Double.Parse(objToken["usd"].ToString());
                BTCAmount = Double.Parse(objToken["btc"].ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }


    }
}
